<template>
  <div class="diary-container">
    <!-- 侧边导航栏 -->
    <Sidebar v-if="hasSidebar" />

    <!-- 主内容区 -->
    <div class="main-content">
      <!-- 用户信息卡片 -->
      <div class="user-profile-card">
        <div class="profile-header">
          <el-avatar :size="72" :src="currentUser.avatar || defaultAvatar" />
          <div class="profile-info">
            <h1 class="username">{{ currentUser.name || '未命名用户' }}</h1>
            <div class="meta-info">
              <span class="ip-location">
                <el-icon><Location /></el-icon>
                IP属地：{{ currentUser.ipLocation || '未知' }}
              </span>
            </div>
          </div>
        </div>
      </div>

      <!-- 标签导航 -->
      <el-tabs v-model="activeTab" class="nav-tabs">
        <el-tab-pane name="notes">
          <template #label>
            <span class="tab-label">
              <el-icon><Notebook /></el-icon>
              我的笔记 · {{ myNotes.length }}
            </span>
          </template>
          
          <!-- 笔记卡片列表 -->
          <div class="card-grid">
            <div 
              v-for="note in myNotes"
              :key="note.id"
              class="diary-card"
              @click="handleViewDetail(note.id)"
            >
              <div class="card-header">
                <span class="spot-tag">
                  <el-icon><Location /></el-icon>
                  {{ getSpotName(note.spotId) }}
                </span>
                <!-- <el-button
                  type="danger"
                  :icon="note.isLiked ? StarFilled : Star"
                  circle
                  @click.stop="toggleLike(note)"
                /> -->
              </div>
              
              <el-image
                :src="note.cover || defaultCover"
                class="card-cover"
                fit="cover"
              />
              
              <div class="card-body">
                <h3 class="title">{{ note.title || '未命名笔记' }}</h3>
                <div class="stats">
                  <span class="stat-item">
                    <el-icon><View /></el-icon>
                    {{ note.views || 0 }}
                  </span>
                  <span class="stat-item">
                    <el-icon><ChatDotRound /></el-icon>
                    {{ note.comments || 0 }}
                  </span>
                  <span class="stat-item">
                    <el-icon><Star /></el-icon>
                    {{ note.likes || 0 }}
                  </span>
                </div>
              </div>
              <div class="card-actions">
                <el-button 
                  class="action-btn like-btn"
                  :class="{ 'liked': note.isLiked }"
                  @click.stop="toggleLike(note)"
                >
                  <el-icon><StarFilled /></el-icon>
                  {{ note.isLiked ? '已喜欢' : '喜欢' }}
                </el-button>
                
                <el-button 
                  class="action-btn rewrite-btn"
                  @click.stop="handleRewrite(note.id)"
                >
                  <el-icon><Edit /></el-icon>
                  再写一篇
                </el-button>
              </div>
            </div>
          </div>
        </el-tab-pane>

        <el-tab-pane name="likes">
          <template #label>
            <span class="tab-label">
              <el-icon><StarFilled /></el-icon>
              我的点赞 · {{ myLikes.length }}
            </span>
          </template>
          
          <!-- 点赞内容列表 -->
          <div v-if="myLikes.length > 0" class="card-grid">
            <div 
              v-for="like in myLikes"
              :key="like.id"
              class="diary-card"
            >
              <!-- 类似笔记卡片结构 -->
              <div class="card-header">
                <span class="spot-tag">
                  <el-icon><Location /></el-icon>
                  {{ getSpotName(note.spotId) }}
                </span>
                <el-button
                  type="danger"
                  :icon="note.isLiked ? StarFilled : Star"
                  circle
                  @click.stop="toggleLike(note)"
                />
              </div>
              
              <el-image
                :src="note.cover || defaultCover"
                class="card-cover"
                fit="cover"
              />
              
              <div class="card-body">
                <h3 class="title">{{ note.title || '未命名笔记' }}</h3>
                <div class="stats">
                  <span class="stat-item">
                    <el-icon><View /></el-icon>
                    {{ note.views || 0 }}
                  </span>
                  <span class="stat-item">
                    <el-icon><ChatDotRound /></el-icon>
                    {{ note.comments || 0 }}
                  </span>
                  <span class="stat-item">
                    <el-icon><Star /></el-icon>
                    {{ note.likes || 0 }}
                  </span>
                </div>
              </div>
            </div>
          </div>
          <div v-else class="empty-state">
            <el-empty description="暂无点赞内容" />
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { 
  Location, Notebook, Star, StarFilled, 
  View, ChatDotRound 
} from '@element-plus/icons-vue'
import Sidebar from '@/components/Sidebar.vue'

// 默认图片
const defaultAvatar = '/src/assets/default-avatar.png'
const defaultCover = '/src/assets/default-cover.jpg'

// 响应式数据
const activeTab = ref('notes') // 修复：正确定义activeTab
const hasSidebar = ref(true)
const router = useRouter()

// 用户数据
const currentUser = ref({
  name: '旅行达人小明',
  avatar: defaultAvatar, // 使用本地默认图片
  ipLocation: '北京'
})

// 景点数据（模拟）
const attractions = ref({
  1: { id: 1, name: '故宫' },
  2: { id: 2, name: '颐和园' },
  3: { id: 3, name: '北京邮电大学' }
})

// 笔记数据
const myNotes = ref([
  {
    id: 1,
    title: '故宫深度游览指南',
    cover: defaultCover, // 使用本地默认图片
    spotId: 1,
    likes: 128,
    views: 500,
    comments: 15,
    isLiked: false
  }
])

// 点赞数据
const myLikes = ref([
])

// 获取景点名称
const getSpotName = (spotId) => {
  return attractions.value[spotId]?.name || '未知景点'
}

// 互动操作
const toggleLike = (note) => {
  note.isLiked = !note.isLiked
  note.likes += note.isLiked ? 1 : -1
}

// 查看详情
const handleViewDetail = (id) => {
  router.push(`/diary-detail/${id}`)
}

const handleRewrite = (noteId) => {
  console.log('再写一篇，基于笔记ID:', noteId)
  // 这里可以跳转到编辑页面，携带原笔记ID
  // router.push({ path: '/edit', query: { fromNote: noteId } })
}

</script>

<style scoped lang="scss">
// 添加多行文本省略 mixin
@mixin text-ellipsis($lines) {
  display: -webkit-box;
  -webkit-line-clamp: $lines;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.diary-container {
  display: flex;
  min-height: 100vh;
  background: #f8f8f8;
}

.main-content {
  flex: 1;
  padding: 2rem;
  max-width: 1200px;
  margin-left: 17%;
}

.user-profile-card {
  background: white;
  border-radius: 1rem;
  padding: 2rem;
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
  margin-bottom: 2rem;

  .profile-header {
    display: flex;
    align-items: center;
    gap: 1.5rem;

    .profile-info {
      .username {
        font-size: 1.5rem;
        margin: 0 0 0.5rem 0;
      }

      .meta-info {
        color: #666;
        font-size: 0.9rem;
      }
    }
  }
}

.nav-tabs {
  :deep(.el-tabs__nav-wrap) {
    display: flex;
    justify-content: center;
  }
  
  :deep(.el-tabs__nav-scroll) {
    display: flex;
    justify-content: center;
  }
  
  :deep(.el-tabs__nav) {
    float: none;
    display: inline-flex;
  }
  
  :deep(.el-tabs__header) {
    margin: 0 0 2rem 0;
    text-align: center;
  }

  :deep(.el-tabs__item) {
    font-size: 1rem;
    padding: 0 1.5rem;
    height: 3rem;
    display: flex;
    align-items: center;

    .tab-label {
      display: flex;
      align-items: center;
      gap: 0.5rem;
    }
  }
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 1.5rem;
}

.diary-card {
  background: white;
  border-radius: 0.75rem;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
  transition: transform 0.3s ease;

  &:hover {
    transform: translateY(-4px);
    cursor: pointer;
  }

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    
    .spot-tag {
      background: rgba(0,0,0,0.05);
      padding: 0.25rem 0.75rem;
      border-radius: 1rem;
      font-size: 0.9rem;
    }
  }

  .card-cover {
    width: 100%;
    height: 200px;
    object-fit: cover;
  }

  .card-body {
    padding: 1rem;

    .title {
      font-size: 1.1rem;
      margin: 0 0 1rem 0;
      line-height: 1.4;
      @include text-ellipsis(2);
    }

    .stats {
      display: flex;
      gap: 1rem;
      color: #666;
      font-size: 0.9rem;

      .stat-item {
        display: flex;
        align-items: center;
        gap: 0.25rem;
      }
    }
  }
}

.empty-state {
  padding: 3rem 0;
  text-align: center;
  background: white;
  border-radius: 0.75rem;
}

/* 新增按钮样式 */
.card-actions {
  position: absolute;
  right: 1rem;
  bottom: 1rem;
  display: flex;
  gap: 0.5rem;
  z-index: 2;
  
  .action-btn {
    transition: all 0.3s ease;
    border-radius: 20px;
    padding: 8px 16px;
    font-size: 0.85rem;
    
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    }
    
    &:active {
      transform: translateY(0);
    }
  }
  
  .like-btn {
    background: rgba(255,255,255,0.9);
    border: 1px solid #ff4d4f;
    color: #ff4d4f;
    
    &.liked {
      background: #ff4d4f;
      color: white;
      animation: pulse 0.5s ease;
    }
  }
  
  .rewrite-btn {
    background: rgba(255,255,255,0.9);
    border: 1px solid #52c41a;
    color: #52c41a;
  }
}

/* 心跳动画 */
@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

/* 调整卡片样式以容纳按钮 */
.diary-card {
  position: relative;
  padding-bottom: 60px; /* 为按钮留出空间 */
  
  .card-body {
    padding-bottom: 50px; /* 防止文字与按钮重叠 */
  }
}
</style>